/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <fcntl.h>
#include <vector>
#include <string>
#include <dirent.h>
#include <sys/stat.h>
#include <regex>
#include <iostream>
#include <fstream>
#include "It_process_plimits.h"

static int OpenDir(std::string path)
{
    DIR *dirp = opendir(path.c_str());
    (void)closedir(dirp);
    ICUNIT_ASSERT_NOT_EQUAL(dirp, NULL, -1);
    return 0;
}

static int RmPlimitsDir(std::string plimitspath)
{
    int ret = 0;
    (void)RmdirControlFile(plimitspath.c_str());
    ret = rmdir(plimitspath.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;
}

static UINT32 Testcase(void)
{
    int ret;
    mode_t mode;
    (void)OpenDir("/proc/plimits/");
    std::string plimitsTestPath1 = "/proc/plimits/test1";
    std::string plimitsTestPath2 = "/proc/plimits/test2";
    ret = mkdir(plimitsTestPath1.c_str(), S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    ret = mkdir(plimitsTestPath2.c_str(), S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    std::vector<std::string> filePath;
    filePath.push_back("plimits.limiter_add");
    filePath.push_back("plimits.limiter_delete");
    filePath.push_back("plimits.procs");
    filePath.push_back("plimits.limiters");
    filePath.push_back("pids.max");
    filePath.push_back("sched.period");
    filePath.push_back("sched.quota");
    filePath.push_back("sched.stat");
    filePath.push_back("memory.failcnt");
    filePath.push_back("memory.limit");
    filePath.push_back("memory.peak");
    filePath.push_back("memory.usage");
    filePath.push_back("memory.stat");
    std::vector<std::string> filePathR;
    filePathR.push_back("memory.failcnt");
    filePathR.push_back("memory.limit");
    filePathR.push_back("memory.peak");
    filePathR.push_back("memory.usage");
    filePathR.push_back("memory.stat");
    std::string buf = "memory";
    std::string fullPathR = plimitsTestPath1 + "/" + "plimits.limiter_delete";
    ret = WriteFile(fullPathR.c_str(), buf.c_str());
    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, -1);
    for (auto iter = filePathR.begin(); iter != filePathR.end(); ++iter) {
        std::string fullPath = plimitsTestPath1 + "/" + *iter;
        int fd = open(fullPath.c_str(), O_RDONLY);
        ICUNIT_ASSERT_EQUAL(fd, -1, -1);
        (void)close(fd);
    }
    for (auto iter = filePath.begin(); iter != filePath.end(); ++iter) {
        std::string fullPath = plimitsTestPath2 + "/" + *iter;
        int fd = open(fullPath.c_str(), O_RDONLY);
        ICUNIT_ASSERT_NOT_EQUAL(fd, -1, -1);
        (void)close(fd);
    }
    (void)RmPlimitsDir(plimitsTestPath1);
    (void)RmPlimitsDir(plimitsTestPath2);
    return 0;
}

void ItProcessPlimits054(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_054", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}
